package com.freight.msg.controller;

import com.freight.core.base.FreightBaseController;
import com.freight.core.common.constant.CommonConstant;
import com.freight.core.common.utils.PageUtil;
import com.freight.core.common.utils.ResultUtil;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.common.vo.PageVo;
import com.freight.core.common.vo.Result;
import com.freight.core.entity.User;
import com.freight.core.service.UserService;
import com.freight.msg.entity.Message;
import com.freight.msg.entity.MessageSend;
import com.freight.msg.service.MessageSendService;
import com.freight.msg.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @describe
 */
@Slf4j
@RestController
    @Api(description = "消息发送管理接口")
@RequestMapping("/freight/messageSend")
@Transactional
public class MsgMessageSendController extends FreightBaseController<MessageSend, Long> {

    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageSendService messageSendService;

    @Override
    public MessageSendService getService() {
        return messageSendService;
    }

    @Autowired
    private SecurityUtil securityUtil;


    @RequestMapping(value = "/getNewMessageNotice", method = RequestMethod.POST)
    @ApiOperation(value = "获取平台最新一条公告")
    public Result<MessageSend> getNewMessageNotice() {
        MessageSend messageSend = messageSendService.getNewMessageNotice();
        return new ResultUtil<MessageSend>().setData(messageSend);
    }

    @RequestMapping(value = "/getNewMessageSend", method = RequestMethod.POST)
    @ApiOperation(value = "获取最新一条消息")
    public Result<MessageSend> getNewMessageSend() {

        MessageSend messageSend = messageSendService.getNewMessageSend();

        return new ResultUtil<MessageSend>().setData(messageSend);
    }

    @RequestMapping(value = "/modifyReadState", method = RequestMethod.POST)
    @ApiOperation(value = "修改消息读取状态")
    public Result<String> modifyReadState(Long mesId) {
        messageSendService.modifyReadState(mesId);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/statisticsUnreadMessage", method = RequestMethod.GET)
    @ApiOperation(value = "获取未读数量")
    public Result<Long> statisticsUnreadMessage() {
        long quantity = messageSendService.statisticsUnreadMessage();
        return new ResultUtil<Long>().setData(quantity);
    }

    @RequestMapping(value = "/getAppMessageSendList", method = RequestMethod.GET)
    @ApiOperation(value = "app端获取消息")
    public Result<Page<MessageSend>> getAppMessageSendList(PageVo pv) {

        Page<MessageSend> page = messageSendService.getAppMessageSendList(PageUtil.initPage(pv));
        page.getContent().forEach(item -> {
            User u = userService.get(item.getUserId());
            if (u != null) {
                item.setUsername(u.getUsername());
                item.setNickName(u.getNickName());
            }
            Message m = messageService.get(item.getMessageId());
            if (m != null) {
                item.setTitle(m.getTitle());
                item.setContent(m.getContent());
                item.setType(m.getType());
            }
            item.setStatus(CommonConstant.MESSAGE_STATUS_READ);
            messageSendService.save(item);
        });
        return new ResultUtil<Page<MessageSend>>().setData(page);
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<MessageSend>> getByCondition(MessageSend ms,
                                                    PageVo pv) {

        User user=securityUtil.getCurrUser();
        if(ms!=null&&ms.getStatus()!=null){
            ms.setUserId(user.getId());
        }

        Page<MessageSend> page = messageSendService.findByCondition(ms, PageUtil.initPage(pv));
        page.getContent().forEach(item -> {
            User u = userService.get(item.getUserId());
            if (u != null) {
                item.setUsername(u.getUsername());
                item.setNickName(u.getNickName());
            }
            Message m = messageService.get(item.getMessageId());
            if (m != null) {
                item.setTitle(m.getTitle());
                item.setContent(m.getContent());
                item.setType(m.getType());
            }
        });
        return new ResultUtil<Page<MessageSend>>().setData(page);
    }

    @RequestMapping(value = "/all/{type}", method = RequestMethod.GET)
    @ApiOperation(value = "批量读取消息")
    public Result<Object> batchOperation(@Param("0全部已读 1全部删除已读") @PathVariable Integer type) {
        User u = securityUtil.getCurrUser();
        if (type == 0) {
            messageSendService.updateStatusByUserId(u.getId(), CommonConstant.MESSAGE_STATUS_READ);
        } else if (type == 1) {
            messageSendService.deleteByUserId(u.getId());
        }
        return ResultUtil.success("操作成功");
    }

    @RequestMapping(value = "/getMessageSendNum", method = RequestMethod.POST)
    @ApiOperation(value = "PC端 --- 获取未读消息")
    public Result<String> getMessageSendNum() {

        String messNum = messageSendService.getMessageSendNum();

        return ResultUtil.success(messNum);
    }
}
